package cn.wolfcode.education.service.impl;

import cn.wolfcode.education.domain.Client;
import cn.wolfcode.education.domain.Employee;
import cn.wolfcode.education.domain.Trace;
import cn.wolfcode.education.mapper.ClientMapper;
import cn.wolfcode.education.mapper.TraceMapper;
import cn.wolfcode.education.query.QueryObject;
import cn.wolfcode.education.service.ITraceService;
import cn.wolfcode.education.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TraceServiceImpl implements ITraceService {

    @Autowired
    private TraceMapper traceMapper;
    @Autowired
    private ClientMapper clientMapper;

    public void inset(Trace trace) {

        // 获取客户id
        Long clientId = trace.getClient().getId();
        Client client = new Client();
        client.setId(clientId);
        client.setLastTraceTime(new Date()); // 设置最后跟进时间


        trace.setTraceDate(new Date()); // 设置跟进时间
        // 设置跟进人为当前登录用户
        Employee currentEmp = (Employee) SecurityUtils.getSubject().getPrincipal();
        trace.setTraceEmployee(currentEmp);
        clientMapper.updateForTrace(client);
        traceMapper.insert(trace);
    }

    // 查看跟进列表
    public PageResult query() {
        QueryObject qo = new QueryObject();
        int total = traceMapper.queryForCount(qo);
        List<Trace> rows = traceMapper.queryForList(qo);
        for (Trace row : rows) {
            System.out.println(row);
        }
        return new PageResult(total, rows);
    }

    // 审核跟进
    public void audit(Long traceId, Long level, String auditAssess) {
        traceMapper.audit(traceId, level, auditAssess);
    }
}
